<?php
/**
 * Created by PhpStorm.
 * User: wen
 * Date: 2017/6/21
 * Time: 11:41
 */
namespace Biz;
use Think\Exception;

class Transaction extends \Base\BaseBiz{

    /*
     *创建订单流水
     * 1.更新Order
     */
    public function createTransaction($req,$data){
        /*
         * 更新Order
         */
        if (empty($data['contact'])) {
            return \Orm\Order\OrderClass::CONTACT_NULL;
        }
        if (empty($data['tel'])) {
            return \Orm\Order\OrderClass::TEL_NULL;
        }
        if (empty($data['appointment_time'])) {
            return \Orm\Order\OrderClass::APPOINTMENT_TIME_NULL;
        }
        if (empty($data['adds'])) {
            return \Orm\Order\OrderClass::ADDS_NULL;
        }
        if (empty($req['id'])) {
            return \Orm\Order\OrderClass::ID_NULL;
        }
        $order_cls = new \Orm\Order\OrderClass();
        $order_cls->beginTransaction();
        try{
            //按orderid 查询数据再执行更新
            $order_cls->loadById($req['id']);
            $order_cls['contact'] = $data['contact'];
            $order_cls['tel'] = $data['tel'];
            $order_cls['appointment_time'] = $data['appointment_time'];
            $order_cls['adds'] = $data['adds'];
            $order_cls ->update();

            //创建流水号
            //用uid查询用户信息
            $user_biz = new \Biz\User();
            $user_res = $user_biz->getUidById($req['uid']);
            $transaction = new \Orm\Transaction\TransactionClass();
            $transaction['open_id'] = $user_res['open_id'];
            $transaction['uid'] = $user_res['id'];
            $transaction['status'] = \Orm\Transaction\TransactionClass::STATUS_NEW;
            $transaction['reference'] = $req['id'];
            $transaction['amount'] = $order_cls['total_price'];
            $transaction['platform'] = $data['platform'];
            $transaction['payment'] = $data['payment'];
            $transaction ->insert();
            if ($transaction) {
                $transaction ->loadById($transaction['id']);
                $transaction['out_trade_no'] = $transaction['id'] . date('YmdHis', time()) . mt_rand(0000, 9999);
                $transaction->update();
            }
        }catch (\Exception $e){
            $order_cls->rollBack();
            throw new \Exception($e->getMessage());
        }
        $order_cls->commit();
        return array('order_id'=>$order_cls['id'],'transaction_id'=>$transaction['id'],'out_trade_no'=>$transaction['out_trade_no']);

    }
    /*
     * 使用OrderID查询流水号信息
     */
    public function getTransactionByOrderId($order_id){
        if (empty($order_id)) {
            return \Orm\Order\OrderClass::ID_NULL;
        }
        try{
            $transaction = new \Orm\Transaction\TransactionList();
            $transaction['where'] = sprintf("`reference` = %d", $order_id);
            $transaction['order'] = 'insert_time desc';
            $transaction['limit'] = 1;
            $transaction->load();
            return   $transaction;
        }catch (\Exception $e){
            throw new \Exception($e->getMessage());
        }
    }
}